package com.yanqu.road.server.manger.activity.newyearbag;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.newyearbag.NewYearBagConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.NewYearBagBusiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.List;
import java.util.Map;
import java.util.Random;

public class NewYearBagMgr extends TempMgr {

    private static NewYearBagConfig newYearBagConfig = null;


    public static void reloadActivityData() {
        getLogger().info("reload NewYearBagMgr begin");

        // 拿活动
        List<ActivityInfo> list = NormalActivityMgr.getOpenActivityInfoList(eActivityType.NewYearBag.getValue());
        if (list.isEmpty()) {
            newYearBagConfig = null;
            getLogger().info("reload NewYearBagMgr finish, no activity in show time.");
            return;
        }
        ActivityInfo activityInfo = list.get(0);
        int activityId = activityInfo.getActivityId();

        Map<String, ActivityConfig> configMap = NormalActivityMgr.getActivityConfigMap(activityId);

        NewYearBagConfig tmpConfig = new NewYearBagConfig();
        tmpConfig.setActivityInfo(activityInfo);
        tmpConfig.initActivityConfig(configMap);
        tmpConfig.setDrawConfigList(NewYearBagBusiness.getNewYearBagDrawConfigList(activityId));
        tmpConfig.setLimitConfigList(NewYearBagBusiness.getNewYearBagLimitConfigList(activityId));

        newYearBagConfig = tmpConfig;

        getLogger().info("reload NewYearBagMgr finish, activityId {}.", newYearBagConfig.getActivityInfo().getActivityId());
    }

    public static NewYearBagConfig getNewYearBagConfig() {
        return newYearBagConfig;
    }

    public static ActivityInfo getActivityInfo() {
        if (newYearBagConfig != null) {
            return newYearBagConfig.getActivityInfo();
        }
        return null;
    }

    public static boolean inOpenTime() {
        if (newYearBagConfig != null) {
            long now = System.currentTimeMillis();
            if (now > newYearBagConfig.getCanOpenTime() && now < newYearBagConfig.getActivityInfo().getEndTime() * 1000) {
                return true;
            }
        }
        return false;
    }

    public static boolean isFuDai(int goodsId) {
        if (newYearBagConfig == null) {
            return false;
        }
        return newYearBagConfig.getFuDaiId() == goodsId;
    }

    public static long randomTaskValue(int type, long value) {
        if (newYearBagConfig != null) {
            List<Integer> list = newYearBagConfig.getTaskTypeMap().get(type); // 0类型，1概率，2成功进度，3失败进度
            if (list != null) {

                Random random = new Random(); // 业务上重复不要紧
                long newValue = 0;
                for (long i = 0; i < value; i++) {

                    // 成功
                    if (random.nextInt(1000) < list.get(1)) {
                        return list.get(2);
                    }

                    // 失败
                    newValue += list.get(3);
                    if (newValue >= list.get(2)) {
                        return list.get(2);
                    }

                }
                return newValue;
            }
        }

        // 没配置概率的任务，都按原来的处理
        return value;
    }

    public static boolean inLoginRewardTime() {
        if (newYearBagConfig != null) {
            ActivityInfo activityInfo = newYearBagConfig.getActivityInfo();
            if (ActivityMgr.activityInTime(activityInfo)) {
                long now = System.currentTimeMillis();
                int day = (int) ((now - activityInfo.getBeginTime() * 1000) / DateHelper.DAY_MILLIONS + 1);
                // 天数限制
                if (day >= 1 && day <= newYearBagConfig.getLoginRewardDay()) {
                    long subTime = now - DateHelper.getZeroTimeStamp(now);
                    List<Long> timeList = newYearBagConfig.getLoginRewardTimeList();
                    // 时间限制
                    if (timeList.get(0) <= subTime && subTime < timeList.get(1)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
